//
//  DUCalDayButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/12/1.
//

import Cocoa

@IBDesignable
class DUCalDayButton: BaseViewButton {

    private var holiday: CATextLayer!
    private var solarDay: CATextLayer!
    private var lunarDay: CATextLayer!
    private var backDay: DULabel!
    private var isCurrentMonth: Bool = true
    
    var cornerRadius:CGFloat = -1
    
    private var defaultStyle = CalDayStyle.init()
    
    private var mouseInEnteredStyle = CalDayStyle.init()
    private var mouseInDownStyle = CalDayStyle.init()

    
    var date: DateEntity?
    
    private override init() {
        super.init()
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        let w = self.frame.width
        let h = self.frame.height
        solarDay = createLayer(text: "28", size: h * 0.5, frame: NSMakeRect(0, 0, w, h * 0.6))
        lunarDay = createLayer(text: "抗战胜利", size: h * 0.24, frame: NSMakeRect(0, h * 0.55, w, h * 0.4))
        holiday = createLayer(text: "班", size: h * 0.15, frame: NSMakeRect(1, 1, h * 0.25, h * 0.25))
        backDay = DULabel(frame: NSMakeRect(0, 0, w, h))
        backDay.stringValue = ""
        backDay.drawsBackground = true
        backDay.isHidden = true
        backDay.backgroundColor = NSColor(white: 1, alpha: 0.3)
        addSubview(backDay)
    }
    
    private func createLayer(text:String, size:CGFloat, frame: NSRect) -> CATextLayer {
        let s = DUTextLayer()
        s.contentsScale = 3
        s.string = text
        s.alignmentMode = .center
        s.fontSize = size
        s.foregroundColor = NSColor.black.cgColor
        s.frame = frame
        return s
    }
    
    override func layout() {
        self.layer?.addSublayer(solarDay)
        self.layer?.addSublayer(lunarDay)
        self.layer?.addSublayer(holiday)
        setDefaultStyle(style: self.defaultStyle)
        setMouseEnterStyle()
        setMouseDownStyle()
        stateChange()
        
    }
    
    override var isFlipped: Bool {
        return true
    }
    
    func setDate(date: DateEntity) {
        self.date = date
        self.dayType = date.dayType
        solarDay.string = String(date.solar.day)
        lunarDay.string = date.lunar.dayToMonthStr
        self.notMonth = !date.currentMonth
        stateChange()
    }
    
    func setFestival() {
        self.defaultStyle.lunarForegroundColor = CGColor(red: 0.96, green: 0.2, blue: 0.2, alpha: 1)
        self.setMouseEnterStyle()
        self.setMouseDownStyle()
        lunarDay.foregroundColor = self.defaultStyle.lunarForegroundColor
    }
    
    func setDefaultStyle(style: CalDayStyle) {
        solarDay.foregroundColor = style.solarForegroundColor
        lunarDay.foregroundColor = style.lunarForegroundColor
        holiday.foregroundColor = style.holidayForegroundColor
        holiday.isHidden = style.holidayIsHidden
        backDay.backgroundColor = style.maskColor
        backDay.isHidden = style.maskIsHidden
        self.layer?.backgroundColor = style.backgroundColor
        self.alphaValue = style.alphaValue
        
        self.layer?.cornerRadius = style.cornerRadius
        self.layer?.borderWidth = style.borderWidth
        self.layer?.borderColor = style.borderColor
    }
    
    var dayType: DateEntity.DayType = .general {
                didSet {
            switch dayType {
                case .general :
                    self.defaultStyle = CalDayStyle.defaultStyle
                case .holiday :
                    self.holiday.string = "休"
                    self.defaultStyle = CalDayStyle.holidayStyle
                case .work :
                    self.holiday.string = "班"
                    self.defaultStyle = CalDayStyle.workStyle
                case .week :
                    self.defaultStyle = CalDayStyle.weekStyle
                case .currentDay:
                    self.defaultStyle = CalDayStyle.currentStyle
                case .festvial:
                    break
            }
            if self.cornerRadius != -1 {
                self.defaultStyle.cornerRadius = self.cornerRadius
            }
            setMouseEnterStyle()
            setMouseDownStyle()
            setDefaultStyle(style: self.defaultStyle)
        }
    }
    
    var notMonth: Bool {
        get {
            return !isCurrentMonth
        }
        set {
            self.isCurrentMonth = !newValue
            let a:CGFloat = newValue ? 0.5 : 1
            self.defaultStyle.alphaValue = a
            self.setMouseEnterStyle()
            self.setMouseDownStyle()
            self.alphaValue = a
            self.backDay.isHidden = newValue
        }
    }
    
    func setNil () {
        solarDay.string = ""
        lunarDay.string = ""
        backDay.isHidden = true
        self.layer?.backgroundColor = nil
    }
    
    override func mouseInEnterd(sender: BaseViewButton, event: NSEvent) {
        if self.state != .off {
            return
        }
        setDefaultStyle(style: self.mouseInEnteredStyle)
    }
    
    override func mouseInExited(sender: BaseViewButton, event: NSEvent) {
        if self.state != .off {
            return
        }
        setDefaultStyle(style: self.defaultStyle)
    }
    
    override func mouseInDown(sender: BaseViewButton, event: NSEvent) {
        //setDefaultStyle(style: self.mouseInDownStyle)
    }
    
    override func stateChange() {
        if allowsState {
            switch state {
                case .off:
                    dayType = date != nil ? date!.dayType : .general
                case .on:
                    setFestival()
                case .holiday:
                    dayType = .holiday
                case .work:
                    dayType = .work
            }
            setMouseEnterStyle()
            setMouseDownStyle()
            if state != .off {
                setDefaultStyle(style: self.mouseInDownStyle)
            }
            
            return
        }
        if self.state == .on {
            setDefaultStyle(style: self.mouseInDownStyle)
        } else {
            setDefaultStyle(style: self.defaultStyle)
        }
        
    }
    
    override func enableIsHander(sender: BaseViewButton, enable: Bool) {
         
    }

    private func setMouseEnterStyle() {
        self.mouseInEnteredStyle = self.defaultStyle
        self.mouseInEnteredStyle.borderWidth = 2
        self.mouseInEnteredStyle.borderColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.5)
    }
    private func setMouseDownStyle() {
        self.mouseInDownStyle = self.defaultStyle
        self.mouseInDownStyle.borderWidth = 2
        self.mouseInDownStyle.borderColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 1)
    }
    
    struct CalDayStyle {
        var solarForegroundColor:CGColor?   //阳历字体颜色
        var lunarForegroundColor:CGColor?   //农历字体颜色
        var holidayForegroundColor:CGColor? //假期字体颜色
        var holidayIsHidden:Bool = true     //休假加班标识是否隐藏
        var backgroundColor:CGColor?        //背景颜色
        var alphaValue:CGFloat = 1          //透明度 *通常用于非本月日期显示
        var borderColor: CGColor?           //边框颜色
        var borderWidth: CGFloat = 0        //边框宽度
        var cornerRadius: CGFloat = 5       //圆角值
        
        var maskColor:NSColor?              //蒙板背景颜色
        var maskIsHidden:Bool = true        //蒙板是否隐藏
        init() {
        }
        
    }
}
fileprivate extension DUCalDayButton.CalDayStyle {
    private static var BLACK_COLOR = CGColor(red: 0, green: 0, blue: 0, alpha: 1)
    private static var RED_COLOR = CGColor(red: 0.96, green: 0.2, blue: 0.2, alpha: 1)
    /**
     默认样式，通常为当月工作日显示样式
     */
    static var defaultStyle: DUCalDayButton.CalDayStyle {
        get {
            var d = DUCalDayButton.CalDayStyle()
            d.solarForegroundColor = BLACK_COLOR
            d.lunarForegroundColor = BLACK_COLOR
            d.holidayIsHidden = true
            d.maskIsHidden = true
            d.backgroundColor = nil
            d.alphaValue = 1
            return d
        }
    }
    
    static var currentStyle: DUCalDayButton.CalDayStyle {
        get {
            var d = DUCalDayButton.CalDayStyle()
            d.solarForegroundColor = BLACK_COLOR
            d.lunarForegroundColor = BLACK_COLOR
            d.holidayIsHidden = true
            d.maskIsHidden = true
            d.backgroundColor = nil
            d.alphaValue = 1
            return d
        }
    }
    
    /**
     周末样式
     */
    static var weekStyle:DUCalDayButton.CalDayStyle {
        get {
            var d = DUCalDayButton.CalDayStyle()
            d.solarForegroundColor = RED_COLOR//BLACK_COLOR
            d.lunarForegroundColor = BLACK_COLOR
            d.holidayIsHidden = true
            d.maskIsHidden = true
            d.backgroundColor = nil
            d.alphaValue = 1
            return d
        }
    }
    
    /**
     节假日样式
     */
    static var holidayStyle:DUCalDayButton.CalDayStyle {
        get {
            var d = DUCalDayButton.CalDayStyle()
            d.solarForegroundColor = RED_COLOR//BLACK_COLOR
            d.lunarForegroundColor = BLACK_COLOR
            d.holidayIsHidden = false
            d.holidayForegroundColor = CGColor(red: 0.96, green: 0.19, blue: 0.19, alpha: 1)
            d.maskIsHidden = true
            d.backgroundColor = CGColor(red: 0.98, green: 0.89, blue: 0.89, alpha: 0.7)
            d.alphaValue = 1
            return d
        }
    }

    /**
     加班倒休日样式
     */
    static var workStyle:DUCalDayButton.CalDayStyle {
        get {
            var d = DUCalDayButton.CalDayStyle()
            d.solarForegroundColor = BLACK_COLOR
            d.lunarForegroundColor = BLACK_COLOR
            d.holidayIsHidden = false
            d.holidayForegroundColor = CGColor(red: 0.36, green: 0.4, blue: 0.45, alpha: 1)
            d.maskIsHidden = true
            d.backgroundColor = CGColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.7)
            d.alphaValue = 1
            return d
        }
    }
}
 
